// pages/collection/collection.js
const app = getApp();
var service = require('../../util/service');
var util = require('../../util/util')
var QRCode = require('../../util/weapp-qrcode.js')
var qrcode;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head: {
      currentCity: "",
      placestr: '查找品牌或商品'
    },
    selOrg: true,
    couponList: [],
    headline: [],
    cardList: [],
    qrcodeShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getpol(
      function (currentCity) {
        that.setData({
          'head.currentCity': currentCity
        })
      }
    )
    var that = this;
    qrcode = new QRCode('canvas', {
      text: "code=0000000000000",
      width: 150,
      height: 150,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
    var user = app.userInfo;
    service.AJAX(`smc/buy/CouponRecipients/listCouponRecipients.json?$userId=${user.buyId}`, {}, res => {
      var res2 = res || [];
      var couids = [];
      for(var i=0;i<res2.length;i++){
        couids.push(res2[i].voucherId);
      }
      service.AJAX(`smc/buy/CouponVoucher/listCouponVoucher.json?$couponId-in=${couids.toString()}`, {}, res => {
        var res = res || [];
        for (var i = 0; i < res.length; i++) {
          res[i].effeDateFrom = util.getDate(res[i].effeDateFrom);
          res[i].effeDateEnd = util.getDate(res[i].effeDateEnd);
        }
        that.setData({
          couponList: res,
        })
      }, err => { }, 'GET', true)
    }, err => { }, 'GET', true)
  },
  tapqrcode: function () {
    this.setData({
      qrcodeShow: false
    })
  },
  getcoupon:function (e) {
    //e.target.dataset.code
    qrcode.makeCode("这里是一段测试扫描使用优惠卷二维码");
    this.setData({
      qrcodeShow: true
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selindex: function () {
    this.setData({
      'selOrg': true
    })
  },
  selindex2: function () {
    this.setData({
      'selOrg': false
    })
  }
})